
const wx2my = require('../../wx2my');

var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');

var demo = new QQMapWX({
  key: '3NGBZ-BLF6N-RXPFK-SPZSN-2EXFE-KWFUW' //申请的Key

});

const api = require('../../utils/user_api_miniprogram.js');

const app = getApp();


Page({
  data: {
    InviteCode:"",
    activity: {},
    helpList: [],
    ruleList: [],
    timer: null,
    endcountdown: "",
    had_help: "",
  },

  onUnload: function () {
    if (this.data.timer) {
      clearInterval(this.data.timer);
    }
  },

  onLoad(e) {

    console.log(e)
    let that = this;

    if (e.InviteCode) {
      that.setData({
        InviteCode: e.InviteCode
      });
    }

    if (e.q) {
      if (e.q.indexOf('InviteCode') > 0) {
        let url = decodeURIComponent(e.q);
        let InviteCode = app.fm.getUrlParam('InviteCode', url);
        that.setData({
          InviteCode: InviteCode
        });

      }

    }

    that.getHelpActivityByInviteCode(that.data.InviteCode)

  },

  
  getHelpActivityByInviteCode(InviteCode) {
    let that = this;

    app.wxRequest('getHelpActivityByInviteCode', {
      'token': wx2my.getStorageSync('token'),
      "InviteCode": InviteCode,

    }, function (res) {
      console.log(res);
      if (res.data.code == 1) {
        that.setData({
          activity: res.data.data.activity,
          helpList: res.data.data.helpList,
          ruleList: res.data.data.ruleList,
          had_help: res.data.data.had_help,

        });
        that.startActivityCountdown(that.data.activity.end_time);
      }

    });
  },
  tohelpPage(){
    wx2my.reLaunch({
      url: '/pages/home/home'
    });
  },
  tohelpit(){
    let that = this;

    app.wxRequest('helpUserActivity', {
      'token': wx2my.getStorageSync('token'),
      "InviteCode": this.data.InviteCode,
      "source": "支付宝"
    }, function (res) {
      console.log(res);
      if (res.data.code == 1) {
        wx2my.showToast({
          title: '助力成功！',
          icon: 'success',
          duration: 2000,
          success() {}
        });

        that.setData({
          had_help: true

        });

      }else{
        
        wx2my.showToast({
          title: res.data.msg,
          icon: 'error',
          duration: 2000,

          success() {}

        });
      }

    });


  },

  startActivityCountdown(endTime) {
    const end = new Date(endTime.replace(/-/g, '/')).getTime();
    if (this.data.timer) {
      clearInterval(this.data.timer);
    }

    this.data.timer = setInterval(() => {
      const now = new Date().getTime();
      const distance = end - now;


      if (distance <= 0) {
        clearInterval(this.data.timer);
        this.setData({ endcountdown: "00:00:00" });
        return;
      }

      // 计算时、分、秒
      const hours = Math.floor(distance / (1000 * 60 * 60));
      const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
      const seconds = Math.floor((distance % (1000 * 60)) / 1000);

      // 格式化为两位数（如 5 → "05"）
      const formatTime = (num) => num < 10 ? `0${num}` : num;

      this.setData({
        endcountdown: `${formatTime(hours)}:${formatTime(minutes)}:${formatTime(seconds)}`
      });
      
    }, 1000);
  },
  

});
 